﻿using System.IO;
using UnityEngine;

public class SaveData
{
    public const int STAR_NUM = 12;
    public int UnlockSceneIndex {  get; set; }  //当前解锁的关卡
    public int SceneIndex { get; set; }         //当前到达的关卡
    public int CheckPointIndex { get; set; }    //当前到达的存档点
    public bool[] Stars_Destroyed { get; set; } //星星收集情况

    public SaveData()
    {
        UnlockSceneIndex = 1;
        SceneIndex = 1;
        CheckPointIndex = 0;
        Stars_Destroyed = new bool[STAR_NUM];
    }
}

[CreateAssetMenu]
public class SaveManager : ScriptableObject
{
    public SaveData SaveData { get; private set; }
    private string savePath;
    public int Point
    {
        get
        {
            int count = 0;
            for (int i = 0; i < SaveData.STAR_NUM; i++)
            {
                if (SaveData.Stars_Destroyed[i])
                    count++;
            }
            return count;
        }
    }
    
    public void Initialize()
    {
        savePath = Application.persistentDataPath + "/autosave";
        SaveData = new SaveData();
        Load();
    }
    
    public void Save()
    {
        using (BinaryWriter writer = new BinaryWriter(File.Open(savePath, FileMode.Create)))
        {
            writer.Write(SaveData.UnlockSceneIndex);
            writer.Write(SaveData.SceneIndex);
            writer.Write(SaveData.CheckPointIndex);
            for (int i = 0; i < SaveData.STAR_NUM; i++)
            {
                writer.Write(SaveData.Stars_Destroyed[i]);
            }   
        }
        ;
    }
    
    public void Load()
    {
        try
        {
            using (BinaryReader reader = new BinaryReader(File.OpenRead(savePath)))
            {
                SaveData.UnlockSceneIndex = reader.ReadInt32();
                SaveData.SceneIndex = reader.ReadInt32();
                SaveData.CheckPointIndex = reader.ReadInt32();
                for (int i = 0; i < SaveData.STAR_NUM; i++)
                {
                    SaveData.Stars_Destroyed[i] = reader.ReadBoolean();
                }
            }
        }
        catch
        {
            Debug.LogWarning("存档文件损坏或缺失");
            Save();
        }
    }

    public void ResetGame()
    {
        SaveData = new SaveData();
        Save();
        Load();
    }

    /// <summary>
    /// 更新得分情况
    /// </summary>
    public void UpdateStar(int index, bool destroyed)
    {
        if (index < 0 || index >= SaveData.STAR_NUM)
            return;
        SaveData.Stars_Destroyed[index] = destroyed;
        Save();
    }
    /// <summary>
    /// 更新进度
    /// </summary>
    public void UpdateScene(int sceneIndex,int checkPointIndex)
    {
        SaveData.SceneIndex = sceneIndex;
        SaveData.UnlockSceneIndex = Mathf.Max(sceneIndex, SaveData.UnlockSceneIndex);
        SaveData.CheckPointIndex = checkPointIndex;
        Save();
    }

    public void Log()
    {
        Debug.Log($"解锁了{SaveData.UnlockSceneIndex}关，上次结束时在第{SaveData.SceneIndex}关，第{SaveData.CheckPointIndex}个存档点");
    }
}
